Imports System.Text

Public Class RPT_Status
    Private Const Q As String = Chr(34)

    Private mbDataExists As Boolean = False

    Public ReadOnly Property DataExists() As Boolean
        Get
            Return mbDataExists
        End Get
    End Property

    Public Function SchemaChanges( _
        ByVal lSID As Long, _
        ByVal lDBID As Long, _
        ByVal sBASEDIR As String, _
        ByVal sStartDate As String, _
        ByVal sEndDate As String) As String

        Dim sHTM As New StringBuilder

        With sHTM
            '----Logins
            Dim oLoginFactory As New SQLAutoDocLib.Login_Factory
            Dim oLoginList As Collection = oLoginFactory.GetAllLoginsForDatabase(lDBID, sStartDate, sEndDate)
            If oLoginList.Count > 0 Then
                mbDataExists = True

                .Append("<table>")
                .Append("<tr>")
                .Append("<td colspan=" & Q & "3" & Q & ">")
                .Append("<a href=" & Q & "http://" & sBASEDIR & "/SAD/SQLoginE.aspx?S=" & lSID & "&D=" & lDBID & Q & ">")
                .Append("Logins")
                .Append("</a>")
                .Append("</td>")
                .Append("</tr>")

                .Append("<tr>")
                .Append("<td>Modified</td>")
                .Append("<td>Rev</td>")
                .Append("<td>Name</td>")
                .Append("</tr>")


                For Each oLogin As SQLAutoDocLib.Login In oLoginList
                    .Append("<tr>")
                    .Append("<td>" & oLogin.LastModified & "</td>")
                    .Append("<td>&nbsp;</td>")
                    .Append("<td>" & oLogin.Login & "</td>")
                    .Append("</tr>")
                Next
                .Append("</table>")

                .Append("<br/>")
            End If

            '---Tables
            Dim oTableFactory As New SQLAutoDocLib.Table_Factory
            Dim oTableList As Collection = oTableFactory.GetAllTablesForDatabase(lDBID, sStartDate, sEndDate, SQLAutoDocLib.Table_Factory.SORT.DATE_DESC)
            If oTableList.Count > 0 Then
                mbDataExists = True

                .Append("<table>")
                .Append("<tr>")
                .Append("<td colspan=" & Q & "3" & Q & ">")
                .Append("<a href=" & Q & "http://" & sBASEDIR & "/SAD/SQTableE.aspx?S=" & lSID & "&D=" & lDBID & Q & ">")
                .Append("Tables")
                .Append("</a>")
                .Append("</td>")
                .Append("</tr>")

                .Append("<tr>")
                .Append("<td>Modified</td>")
                .Append("<td>Rev</td>")
                .Append("<td>Name</td>")
                .Append("</tr>")


                For Each oTable As SQLAutoDocLib.Table In oTableList
                    .Append("<tr>")
                    .Append("<td>" & oTable.LastModified & "</td>")
                    .Append("<td>" & oTable.RevID & "</td>")
                    .Append("<td>")
                    .Append("<a href=" & Q & "http://" & sBASEDIR & "/SAD/SQTableD.aspx?S=" & lSID & "&D=" & lDBID & "&O=" & oTable.ID & Q & ">")
                    .Append(oTable.Name)
                    .Append("</a>")
                    .Append("</td>")
                    .Append("</tr>")
                Next
                .Append("</table>")

                .Append("<br/>")
            End If

            '---Views
            Dim oViewFactory As New SQLAutoDocLib.View_Factory
            Dim oViewList As Collection = oViewFactory.GetAllViewsForDatabase(lDBID, sStartDate, sEndDate, SQLAutoDocLib.View_Factory.SORT.DATE_DESC)
            If oViewList.Count > 0 Then
                mbDataExists = True

                .Append("<table>")
                .Append("<tr>")
                .Append("<td colspan=" & Q & "3" & Q & ">")
                .Append("<a href=" & Q & "http://" & sBASEDIR & "/SAD/SQViewE.aspx?S=" & lSID & "&D=" & lDBID & Q & ">")
                .Append("Views")
                .Append("</a>")
                .Append("</td>")
                .Append("</tr>")

                .Append("<tr>")
                .Append("<td>Modified</td>")
                .Append("<td>Rev</td>")
                .Append("<td>Name</td>")
                .Append("</tr>")


                For Each oView As SQLAutoDocLib.View In oViewList
                    .Append("<tr>")
                    .Append("<td>" & oView.LastModified & "</td>")
                    .Append("<td>" & oView.RevID & "</td>")
                    .Append("<td>")
                    .Append("<a href=" & Q & "http://" & sBASEDIR & "/SAD/SQViewD.aspx?S=" & lSID & "&D=" & lDBID & "&O=" & oView.ID & Q & ">")
                    .Append(oView.Name)
                    .Append("</a>")
                    .Append("</td>")
                    .Append("</tr>")
                Next
                .Append("</table>")

                .Append("<br/>")
            End If

            '---Stored Procedures
            Dim oSProcFactory As New SQLAutoDocLib.SProc_Factory
            Dim oSProcList As Collection = oSProcFactory.GetAllSProcsForDatabase(lDBID, sStartDate, sEndDate, SQLAutoDocLib.SProc_Factory.SORT.DATE_DESC)
            If oSProcList.Count > 0 Then
                mbDataExists = True

                .Append("<table>")
                .Append("<tr>")
                .Append("<td colspan=" & Q & "3" & Q & ">")
                .Append("<a href=" & Q & "http://" & sBASEDIR & "/SAD/SQSProcE.aspx?S=" & lSID & "&D=" & lDBID & Q & ">")
                .Append("Stored Procedures")
                .Append("</a>")

                .Append("</td>")
                .Append("</tr>")

                .Append("<tr>")
                .Append("<td>Modified</td>")
                .Append("<td>Rev</td>")
                .Append("<td>Name</td>")
                .Append("</tr>")


                For Each oSProc As SQLAutoDocLib.SProc In oSProcList
                    .Append("<tr>")
                    .Append("<td>" & oSProc.LastModified & "</td>")
                    .Append("<td>" & oSProc.RevID & "</td>")
                    .Append("<td>")
                    .Append("<a href=" & Q & "http://" & sBASEDIR & "/SAD/SQSProcD.aspx?S=" & lSID & "&D=" & lDBID & "&O=" & oSProc.ID & Q & ">")
                    .Append(oSProc.Name)
                    .Append("</a>")
                    .Append("</td>")
                    .Append("</tr>")
                Next
                .Append("</table>")

                .Append("<br/>")
            End If

            '---Functions
            Dim oFunctionFactory As New SQLAutoDocLib.Function_Factory
            Dim oFunctionList As Collection = oFunctionFactory.GetAllFunctionsForDatabase(lDBID, sStartDate, sEndDate, SQLAutoDocLib.Function_Factory.SORT.DATE_DESC)
            If oFunctionList.Count > 0 Then
                mbDataExists = True

                .Append("<table>")
                .Append("<tr>")
                .Append("<td colspan=" & Q & "3" & Q & ">")
                .Append("<a href=" & Q & "http://" & sBASEDIR & "/SAD/SQFunctionE.aspx?S=" & lSID & "&D=" & lDBID & Q & ">")
                .Append("Functions")
                .Append("</a>")
                .Append("</td>")
                .Append("</tr>")

                .Append("<tr>")
                .Append("<td>Modified</td>")
                .Append("<td>Rev</td>")
                .Append("<td>Name</td>")
                .Append("</tr>")

                For Each oFunction As SQLAutoDocLib.Function In oFunctionList
                    .Append("<tr>")
                    .Append("<td>" & oFunction.LastModified & "</td>")
                    .Append("<td>" & oFunction.RevID & "</td>")
                    .Append("<td>")
                    .Append("<a href=" & Q & "http://" & sBASEDIR & "/SAD/SQFunctionD.aspx?S=" & lSID & "&D=" & lDBID & "&O=" & oFunction.ID & Q & ">")
                    .Append(oFunction.Name)
                    .Append("</a>")
                    .Append("</td>")
                    .Append("</tr>")
                Next
                .Append("</table>")
            End If
        End With

        If mbDataExists = False Then
            sHTM.Append("No changes found in period.")
        End If

        Return sHTM.ToString
    End Function
End Class
